package com.cats.activity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;

import com.cats.R;
import com.cats.model.AddressModel;

public class PickupActivity extends BaseActivity implements OnClickListener {
	/**
	 * default address null
	 */
	private String selectedAddress = null;
	/**
	 * default number of item: 1
	 */
	private int numberOfItems = 1;
	/**
	 * default total weight: under 1 lbs. 
	 */
	private float totalWeight = 1.0f;
	
	private final int REQUEST_CODE_ADDRESS = 7;
	
	private EditText dateEditText;
    private int year;
    private int month;
    private int day;
    static final int DATE_DIALOG_ID = 100;

	public PickupActivity() {
		// TODO Auto-generated constructor stub
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pickup);
		System.out.println("[PickupActivity]onCreate");
		setActionBarTitle("Pickup");
		
		/**
		 * init set up buttons callbacks
		 */
		Button btnAddress = (Button)findViewById(R.id.btnAddress);
		btnAddress.setOnClickListener(this);
		Button btnDate = (Button)findViewById(R.id.btnDate);
		btnDate.setOnClickListener(this);
		Button btnSchedule = (Button)findViewById(R.id.btnSchedule);
		btnSchedule.setOnClickListener(this);
		
		/**
		 * init
		 */
        setCurrentDate();
	}
	
    // display current date both on the text view and the Date Picker when the application starts.
    public void setCurrentDate() {
    	dateEditText = (EditText)findViewById(R.id.txtDate);
    	
    	
    	long tomorrow = Calendar.getInstance().getTimeInMillis() + 24*60*60*1000;
    	Date tomorrowDate = new Date(tomorrow);
    	
    	DateFormat df = new SimpleDateFormat("MM-d-yyyy");
    	String tomorrowString = df.format(tomorrowDate);
    	dateEditText.setText(tomorrowString);
    	    	
//        final Calendar calendar = Calendar.getInstance();
//        year = calendar.get(Calendar.YEAR);
//        month = calendar.get(Calendar.MONTH);
//        day = calendar.get(Calendar.DAY_OF_MONTH);
//        // set current date into textview
//        dateEditText.setText(new StringBuilder()
//            // Month is 0 based, so you have to add 1
//            .append(month + 1).append("-")
//            .append(day).append("-")
//            .append(year).append(" "));
    }
    
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
           // set date picker as current date
        	DatePickerDialog dialog = new DatePickerDialog(this, datePickerListener, year, month,day);
        	long tomorrow = Calendar.getInstance().getTimeInMillis() + 24*60*60*1000;
        	dialog.getDatePicker().setMinDate(tomorrow);
           return dialog; // new DatePickerDialog(this, datePickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // set selected date into Text View
            dateEditText.setText(new StringBuilder().append(month + 1)
               .append("-").append(day).append("-").append(year).append(" "));
        }
    };

	/**
	 * buttons
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAddress: 
			/**
			 * go to address list to select an address
			 */
			// navigate to addresses page
			final Intent intent = new Intent(PickupActivity.this, AddressListActivity.class);
			intent.putExtra("SOURCE", "com.cats.activity.PickupActivity");
			startActivityForResult(intent, REQUEST_CODE_ADDRESS);			
			break;

		case R.id.btnDate:
            showDialog(DATE_DIALOG_ID);
			break;

		case R.id.btnSchedule:
			EditText addressEditText = (EditText)findViewById(R.id.txtAddress);
			CheckBox checkBox = (CheckBox)findViewById(R.id.checkBox);
			if (addressEditText.getText() == null || addressEditText.getText().toString().equals("")) {
				showWarningWithMsg("You must choose an address.");				
			}
			else if (!checkBox.isChecked()) {
				showWarningWithMsg("You must agree to the Terms and Conditions.");
			}
			else {
				/**
				 * do schedule
				 */
				// TODO:
				
				/**
				 * success, finish and return back to home
				 */
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Thanks! Your pickup has been scheduled!").setCancelable(false)
						.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
							}
						});
				AlertDialog alert = builder.create();
				alert.show();
			}
			break;

		default:
			break;
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE_ADDRESS && resultCode == RESULT_OK){
			/**
			 * success get get selected address
			 */
			Bundle b = data.getExtras();
			AddressModel addressModel = (AddressModel) b.getSerializable("ADDRESS");
			System.out.println("[PickupActivity]:get address selected:" + addressModel.getFullAddress());
			EditText addressEditText = (EditText)findViewById(R.id.txtAddress);
			addressEditText.setText(addressModel.getFullAddress());
        }
	}
}




